﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Collections.Generic;
using Ccit.Global;

namespace Ccit.OA.NewsCenter
{
    public class LayoutSQL  : ILayout
    {
        /// <summary>
        /// 获取页面布局对象
        /// </summary>
        /// <param name="userId">用户ID(NEWS_B1_10)</param>
        /// <returns></returns>
        public MsLayout GetLayOutObj(string userId,string Type)
        {
            string sType = (Type != "") ? Type : "0";
            MsLayout layoutObj = null;
            if (string.IsNullOrEmpty(userId)) return layoutObj;
            string cmdText = " SELECT NEWS_B1_10,NEWS_B1_20,NEWS_B1_30,NEWS_B1_40,NEWS_B1_50 FROM NEWS_B1 WHERE NEWS_B1_10 = '" + userId + "' AND NEWS_B1_60 ='"+sType+"'";

            using (IDataReader ireader = DataAccessor.ExecuteReader(cmdText))
            {
                while(ireader.Read()){
                    layoutObj = new MsLayout();
                    layoutObj.UserId = ireader["NEWS_B1_10"].ToString();
                    layoutObj.CellCount = ireader["NEWS_B1_20"].ToString();
                    layoutObj.OneCellWidth = ireader["NEWS_B1_30"].ToString();
                    layoutObj.TwoCellWidth = ireader["NEWS_B1_40"].ToString();
                    layoutObj.ThreeCellWidth = ireader["NEWS_B1_50"].ToString();
                }
            }
            return layoutObj;
        }

        /// <summary>
        /// 布局设置
        /// </summary>
        /// <param name="layout">布局设置对象</param>
        /// <param name="selOnePlate">一列里板块</param>
        /// <param name="selTwoPlate">二列里板块</param>
        /// <param name="selThreePlate">三列里板块</param>
        /// <returns></returns>
        public List<string> SaveMsLayOut(MsLayout layout, string selOnePlate, string selTwoPlate, string selThreePlate, string unSelPlate, string Type, string SysUserId)
        {
            string sType = (Type != "") ? Type : "0";
            LoginUser login=(LoginUser)HttpContext.Current.Session["logUserInfo"];
            if(SysUserId == "-999")
            {
                CheckSysPlate(sType, login.UserId);
            }

            List<string> strlist = new List<string>();
            
            string hasData = "SELECT COUNT(*) AS HASLOGIN FROM NEWS_B1 WHERE NEWS_B1_10 = '" + login.UserId + "' AND NEWS_B1_60 = '" + sType + "'";
            string hasUser = "";
            using(IDataReader ireader=DataAccessor.ExecuteReader(hasData))
            {
                while(ireader.Read()){
                    hasUser = ireader["HASLOGIN"].ToString();
                }
            }

            if (Convert.ToInt32(hasUser) > 0)    //存在用户
            {
               strlist = doEditMsLayOut(layout,login.UserId,Type);
            }
            else   //不存在用户
            {
               strlist = doAddMsLayOut(layout,login.UserId,Type);
            }

            //修改板块表的显示区域
            if (!string.IsNullOrEmpty(unSelPlate))
            {
                string upSelPlate = " UPDATE NEWS_B2 SET NEWS_B2_40 = 0 WHERE NEWS_B2_10 IN (" + unSelPlate + ") AND NEWS_B2_120 = '" + sType + "' AND NEWS_B2_130 <> '-999' AND NEWS_B2_130 = '"+login.UserId+"'";
                int iexe = DataAccessor.ExecuteNonQuery(upSelPlate);
            }

            if(!string.IsNullOrEmpty(selOnePlate)){
                string upPlate1 = " UPDATE NEWS_B2 SET NEWS_B2_40 = 1 WHERE NEWS_B2_10 IN (" + selOnePlate + ") AND NEWS_B2_120 = '" + sType + "'  AND NEWS_B2_130 <> '-999' AND NEWS_B2_130 = '" + login.UserId + "'";
                int iexe1 = DataAccessor.ExecuteNonQuery(upPlate1);
            }
            if(!string.IsNullOrEmpty(selTwoPlate)){
                string upPlate2 = " UPDATE NEWS_B2 SET NEWS_B2_40 = 2 WHERE NEWS_B2_10 IN (" + selTwoPlate + ") AND NEWS_B2_120 = '" + sType + "'  AND NEWS_B2_130 <> '-999' AND NEWS_B2_130 = '" + login.UserId + "'";
                int iexe2 = DataAccessor.ExecuteNonQuery(upPlate2);
            }
            if(!string.IsNullOrEmpty(selThreePlate)){
                string upPlate3 = " UPDATE NEWS_B2 SET NEWS_B2_40 = 3 WHERE NEWS_B2_10 IN (" + selThreePlate + ") ADN NEWS_B2_120 = '" + sType + "'  AND NEWS_B2_130 <> '-999' AND NEWS_B2_130 = '" + login.UserId + "'";
                int iexe3 = DataAccessor.ExecuteNonQuery(upPlate3);
            }
            

            return strlist;
        }

        /// <summary>
        /// 获取板块中图片板块的信息
        /// </summary>
        /// <returns></returns>
        public List<MsNews> GetPubNews(string Type)
        {
            string sType = (Type != "") ? Type : "0";
            ColumnBLL bll = new ColumnBLL();

            List<MsColumn> lstColumn = bll.GetColListByPurview(sType);  //具有浏览权限的栏目类型
            string columnIds = string.Empty;

            foreach(MsColumn colObj in lstColumn)
            {
                columnIds += (columnIds == "") ? colObj.columnId : "," + colObj.columnId;
            }

            List<MsNews> newsList = new List<MsNews>();
            if (string.IsNullOrEmpty(columnIds))
                return newsList;
            string cmdText = "SELECT NEWS_A3_10 ,NEWS_A3_30,NEWS_A3_50 FROM NEWS_A3 WHERE NEWS_A3_110 = '2' AND  NEWS_A3_120 ='" + sType + "' AND NEWS_A3_20 IN (" + columnIds + ") AND NEWS_A3_50='1' ORDER BY NEWS_A3_9995 DESC";   //信息为已发布的信息
            using(IDataReader ireader=DataAccessor.ExecuteReader(cmdText))
            {
                while(ireader.Read()){
                    MsNews msNew = new MsNews();
                    msNew.NewsId = ireader["NEWS_A3_10"].ToString();  
                    msNew.NewsTitle = ireader["NEWS_A3_30"].ToString();
                    msNew.PicFlag = ireader["NEWS_A3_50"].ToString();
                    newsList.Add(msNew);
                }
            }
            return newsList;
        }
        /// <summary>
        /// 判断当前用户是否有系统板块，没有添加
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool CheckSysPlate(string Type, string userId)
        {
            bool OperResult = false;
            string sType = (Type != "") ? Type : "0";
            //获取所有的系统板块
            string sysWinText = "SELECT NEWS_B3_10,NEWS_B3_20,NEWS_B3_30,NEWS_B3_40,NEWS_B3_50,NEWS_B3_60, NEWS_B3_70,NEWS_B3_80,NEWS_B3_90,NEWS_B3_100,NEWS_B3_110,NEWS_B3_120 FROM NEWS_B3  WHERE NEWS_B3_120 = '"+sType+"'";
            List<MsSysWin> lstSysWin = new List<MsSysWin>();
            using (IDataReader ireader = DataAccessor.ExecuteReader(sysWinText))
            {
                while(ireader.Read()){
                    MsSysWin sysWin = new MsSysWin();
                    sysWin.PlateId = ireader["NEWS_B3_10"].ToString() ;
                    sysWin.PlateName = ireader["NEWS_B3_20"].ToString();
                    sysWin.PlateLogo = ireader["NEWS_B3_30"].ToString();
                    sysWin.ShowArea = ireader["NEWS_B3_40"].ToString();
                    sysWin.OrderNo = ireader["NEWS_B3_50"].ToString();
                    sysWin.TitleStyle = ireader["NEWS_B3_60"].ToString();
                    sysWin.SysFlag = ireader["NEWS_B3_70"].ToString();
                    sysWin.ShowNum = ireader["NEWS_B3_80"].ToString();
                    sysWin.ShowCreateor = ireader["NEWS_B3_90"].ToString();
                    sysWin.ShowPubTime = ireader["NEWS_B3_100"].ToString();
                    sysWin.OtherSet = ireader["NEWS_B3_110"].ToString();
                    sysWin.PlateType = ireader["NEWS_B3_120"].ToString();
                    lstSysWin.Add(sysWin);
                }
            }
            //循环判断个人板块中是否包含系统板块，没有则增加
            if (lstSysWin.Count > 0)
            {
                foreach (MsSysWin sysWin in lstSysWin)
                {
                    string WinText = "SELECT COUNT(*) FROM NEWS_B2 WHERE NEWS_B2_130 = '" + userId + "' AND NEWS_B2_10 = '" + sysWin.PlateId + "' AND NEWS_B2_120 = '"+sType+"'";
                    string hasSys = DataAccessor.ExecuteScalar(WinText).ToString();
                    if(hasSys == "0"){
                        string SaveSys = "INSERT INTO NEWS_B2 (NEWS_B2_10,NEWS_B2_20,NEWS_B2_30,NEWS_B2_40,NEWS_B2_50,NEWS_B2_60,NEWS_B2_70,NEWS_B2_80,NEWS_B2_90,NEWS_B2_100,NEWS_B2_110,NEWS_B2_120,NEWS_B2_130)" +
                                         " VALUES ('"+sysWin.PlateId+"','"+sysWin.PlateName+"','"+sysWin.PlateLogo+"','0','"+sysWin.OrderNo+"','"+sysWin.TitleStyle+"','"+sysWin.SysFlag+"','"+sysWin.ShowNum+"','"+sysWin.ShowCreateor+"','"+sysWin.ShowPubTime+"','"+sysWin.OtherSet+"','"+sysWin.PlateType+"','"+userId+"')";
                        int iexe = DataAccessor.ExecuteNonQuery(SaveSys);
                        OperResult = true;
                    }
                }
            }
            return OperResult;
        }

        /// <summary>
        /// 取新用户的默认布局
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public MsLayout GetLayOutDefault(string Type)
        {
            string sType = (Type != "") ? Type : "0";
            MsLayout layoutObj = null;
            string cmdText = "SELECT NEWS_B1_10,NEWS_B1_20,NEWS_B1_30,NEWS_B1_40,NEWS_B1_50 FROM NEWS_B1 WHERE NEWS_B1_10 = '-999' AND NEWS_B1_60 ='" + sType + "'";
            using (IDataReader ireader = DataAccessor.ExecuteReader(cmdText))
            {
                while (ireader.Read())
                {
                    layoutObj = new MsLayout();
                    layoutObj.UserId = ireader["NEWS_B1_10"].ToString();
                    layoutObj.CellCount = ireader["NEWS_B1_20"].ToString();
                    layoutObj.OneCellWidth = ireader["NEWS_B1_30"].ToString();
                    layoutObj.TwoCellWidth = ireader["NEWS_B1_40"].ToString();
                    layoutObj.ThreeCellWidth = ireader["NEWS_B1_50"].ToString();
                }
            }
            return layoutObj;
        }

        

        #region  私有方法
        //编辑状态
        private List<string> doEditMsLayOut(MsLayout layout,string userId,string Type)
        {
            string sType = (Type != "") ? Type : "0";
            List<string> strlist=new List<string>();
            string cmdText = " UPDATE NEWS_B1 SET NEWS_B1_20 = '" + layout.CellCount + "' , NEWS_B1_30 = '" + layout.OneCellWidth + "' , NEWS_B1_40 = '" + layout.TwoCellWidth + "' , NEWS_B1_50 = '" + layout.ThreeCellWidth + "' WHERE NEWS_B1_10 = '" + userId + "' AND NEWS_B1_60 = '"+sType+"' ";
            if (0 < DataAccessor.ExecuteNonQuery(cmdText))
            {
                strlist.Add("1");
                strlist.Add("保存成功");
                strlist.Add(userId);
            }
            else
            {
                strlist.Add("-1");
                strlist.Add("系统忙,保存失败");
                strlist.Add(userId);
            }
            return strlist;
        }

        //新增状态 
        private List<string> doAddMsLayOut(MsLayout layout, string userId,string Type)
        {
            string sType = (Type != "") ? Type : "0";
            List<string> strlist = new List<string>();
            string[] pubMsg = SessionExt.GetSysFieldSQL("NEWS_B1");
            string cmdText = " INSERT INTO NEWS_B1 (NEWS_B1_10,NEWS_B1_20,NEWS_B1_30,NEWS_B1_40,NEWS_B1_50,NEWS_B1_60," + pubMsg[0] + ") " +
                            " VALUES ('"+userId+"','"+layout.CellCount+"','"+layout.OneCellWidth+"','"+layout.TwoCellWidth+"','"+layout.ThreeCellWidth+"','"+sType+"',"+pubMsg[1]+")";
            if (0 < DataAccessor.ExecuteNonQuery(cmdText))
            {
                strlist.Add("1");
                strlist.Add("保存成功");
                strlist.Add(userId);
            }
            else
            {
                strlist.Add("-1");
                strlist.Add("系统忙,保存失败");
                strlist.Add(userId);
            }
            return strlist;
        }

        #endregion
    }
}
